package com.tencent.ncnnyolox

import android.app.Application
import android.util.Log
import com.air4.chinesetts.tts.TtsManager
import com.alibaba.android.arouter.launcher.ARouter
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.elvishew.xlog.flattener.PatternFlattener
import com.elvishew.xlog.interceptor.BlacklistTagsFilterInterceptor
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.Printer
import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.backup.NeverBackupStrategy
import com.elvishew.xlog.printer.file.naming.ChangelessFileNameGenerator
import com.example.dao.DaoMaster
import com.example.dao.DaoMaster.DevOpenHelper
import com.example.dao.DaoSession
import com.tencent.mmkv.MMKV
import com.tencent.ncnnyolox.basemodule.utils.MMKey
import com.tencent.ncnnyolox.basemodule.utils.MMkvUtils
import com.tencent.ncnnyolox.discern.Message
import java.text.SimpleDateFormat
import java.util.Date

/**
 * <pre>
 * author : yh
 * e-mail : xxx@xx
 * time   : 2023/12/14
 * desc   :
</pre> *
 */
class MyApplication : Application() {
    var daoSession: DaoSession? = null
        private set

    override fun onCreate() {
        super.onCreate()
        application = this
        // 初始化 GreenDao
        val helper = DevOpenHelper(this, "my-db")
        val db = helper.writableDatabase
        val daoMaster = DaoMaster(db)
        daoSession = daoMaster.newSession()

        //判断是否有数据
        val messages = daoSession!!.getMessageDao().loadAll()
        if (messages.size == 0) {
            //初始化数据
            val message = Message("第一条", 0)
            daoSession!!.getMessageDao().insert(message)
        }
        //ARouter.openDebug();
        ARouter.init(this)

        MMKV.initialize(this)
        TtsManager.getInstance().init(this);

        //1730211451122642945
        MMkvUtils.encode(MMKey.memberId,"1730211451122642945")
        MMkvUtils.encode(MMKey.token,
                "MovvS#e51f8e90e3104ab0bf1d33fd8dff5a1c")
        initXlog()
    }

    private fun initXlog() {
        Log.e("initXlog", "initXlog")
        val config = LogConfiguration.Builder()
            .logLevel(
                if (BuildConfig.DEBUG) LogLevel.ALL // Specify log level, logs below this level won't be printed, default: LogLevel.ALL
                else LogLevel.ALL
            )
            .tag("Yolox") // Specify TAG, default: "X-LOG"
            .addInterceptor(
                BlacklistTagsFilterInterceptor( // Add blacklist tags filter
                    "blacklist1", "blacklist2", "blacklist3"
                )
            )
            .build()
        val androidPrinter: Printer =
            AndroidPrinter() // Printer that print the log using android.util.Log
        val filePrinter: Printer =
            FilePrinter.Builder(getExternalFilesDir(null)!!.path) // Specify the path to save log file
                .fileNameGenerator(
                    ChangelessFileNameGenerator(
                        date2String(
                            Date(),
                            "yyyy-MM-dd"
                        ) + ".txt"
                    )
                ) // Default: ChangelessFileNameGenerator("log")
                .backupStrategy(NeverBackupStrategy()) // 指定日志文件备份策略，默认为 FileSizeBackupStrategy(1024 * 1024) NeverBackupStrategy不限制单个日志文件的大小
                .logFlattener(PatternFlattener("{d yyyy-MM-dd HH:mm:ss.SSS} {l}/{t}: {m}")) // Default: DefaultFlattener
                .build()
        XLog.init( // Initialize XLog
            config,  // Specify the log configuration, if not specified, will use new LogConfiguration.Builder().build()
            androidPrinter,  // Specify printers, if no printer is specified, AndroidPrinter(for Android)/ConsolePrinter(for java) will be used.
            filePrinter
        )
    }

    //    /**
    //     * 定时更新欠费公司列表的时间
    //     *
    //     * @return
    //     */
    //    public static Long geterrCompentyUpTime() {
    //        String devID = Common.PDAID;
    //        int h = 11;// 时
    //        int m = 0;// 分
    //        Calendar d = Calendar.getInstance();
    //        try {
    //            int minute = Integer.parseInt(devID.substring(devID.length() - 2,
    //                    devID.length())) % 60;
    //            int minute2 = minute + m;
    //            if (minute2 > 60) {
    //                h = h + 1;
    //                m = minute2 - 60;
    //            } else {
    //                m = minute2;
    //            }
    //            d.setTimeInMillis(System.currentTimeMillis());
    //            d.set(Calendar.HOUR_OF_DAY, h);
    //            d.set(Calendar.MINUTE, m);
    //            d.set(Calendar.SECOND, 0);
    //            d.set(Calendar.MILLISECOND, 0);
    //            if (System.currentTimeMillis() > d.getTimeInMillis()) {
    //                d.set(Calendar.DAY_OF_MONTH, d.get(Calendar.DAY_OF_MONTH) + 1);
    //            }
    //        } catch (Exception e) {
    //            // TODO: handle exception
    //        }
    //        return d.getTimeInMillis();
    //    }
    //    /**
    //     * 定时更新欠费公司列表的时间,延时
    //     *
    //     * @return
    //     */
    //    public static Long geterrCompentyUpTimeDelay() {
    //        long delay=0;
    //        String devID = Common.PDAID;
    //        int h = 11;// 时
    //        int m = 0;// 分
    //        Calendar d = Calendar.getInstance();
    //        try {
    //            int minute = Integer.parseInt(devID.substring(devID.length() - 2,
    //                    devID.length())) % 60;
    //            int minute2 = minute + m;
    //            if (minute2 > 60) {
    //                h = h + 1;
    //                m = minute2 - 60;
    //            } else {
    //                m = minute2;
    //            }
    //            d.setTimeInMillis(System.currentTimeMillis());
    //            d.set(Calendar.HOUR_OF_DAY, h);
    //            d.set(Calendar.MINUTE, m);
    //            d.set(Calendar.SECOND, 0);
    //            d.set(Calendar.MILLISECOND, 0);
    //            if (System.currentTimeMillis() > d.getTimeInMillis()) {
    //                d.set(Calendar.DAY_OF_MONTH, d.get(Calendar.DAY_OF_MONTH) + 1);
    //                delay=d.getTimeInMillis()- System.currentTimeMillis();
    //            }else {
    //                delay=d.getTimeInMillis()- System.currentTimeMillis();
    //            }
    //        } catch (Exception e) {
    //            // TODO: handle exception
    //        }
    //        return delay;
    //    }
    //    /**
    //     * 定时更新欠费员工列表的时间
    //     *
    //     * @return
    //     */
    //    public static Long geterrUserUpTime() {
    //        String devID = Common.PDAID;
    //        int h = 11;// 时
    //        int m = 0;// 分
    //        Calendar d = Calendar.getInstance();
    //        try {
    //            int minute = Integer.parseInt(devID.substring(devID.length() - 2,
    //                    devID.length())) % 60;
    //            int minute2 = minute + m;
    //            if (minute2 > 60) {
    //                h = h + 1;
    //                m = minute2 - 60;
    //            } else {
    //                m = minute2;
    //            }
    //            d.setTimeInMillis(System.currentTimeMillis());
    //            d.set(Calendar.HOUR_OF_DAY, h);
    //            d.set(Calendar.MINUTE, m);
    //            d.set(Calendar.SECOND, 0);
    //            d.set(Calendar.MILLISECOND, 0);
    //            if (System.currentTimeMillis() > d.getTimeInMillis()) {
    //                d.set(Calendar.DAY_OF_MONTH, d.get(Calendar.DAY_OF_MONTH) + 1);
    //            }
    //        } catch (Exception e) {
    //            // TODO: handle exception
    //        }
    //        return d.getTimeInMillis();
    //    }
    fun date2String(date: Date?, format: String?): String {
        return SimpleDateFormat(format).format(date)
    }

    companion object {
        @JvmField
        var application: MyApplication? = null
    }
}